<?php
include_once("../../../common/modules/checkA.php");
include_once("../../../../config.php");
?>


<?


//print_r2($_REQUEST);

//获取当前优惠券的信息

$coupon=select_one_line_all_items($table_prefix."coupon","nid = '".$_REQUEST['coupon_nid']."'");
$member=select_one_line_all_items($table_prefix."member","nid = '".$_REQUEST['member']."'");
$_wechat_member = select_one_line_all_items($table_prefix . "wechat_member", "unionid = '" . $member['unionid'] . "'");


//print_r2($member);

//print_r2($_wechat_member);



$insert_data                   = [];
$insert_data['ai_name']        = $coupon['name'];//优惠券名字
$insert_data['ai_remark']      = $coupon['remark'];//优惠券备注
$insert_data['ai_coupon_type'] = $coupon['coupon_type'];//优惠券类型
$insert_data['ai_is_work']     = "1";//优惠券是否激活

//这个优惠券是折扣还是定额
if ($coupon['coupon_type'] == 10) {
    $insert_data['ai_reduce_price'] = $coupon['reduce_price'];
} else {
    $insert_data['ai_discount'] = $coupon['discount'];
}


//这里应该要根据优惠券的到期类型去到做判断。到底是+日数，还是固定到期时间。
if($coupon['end_time']>0)
{
    $expire_day=$coupon['end_time'];
}
else
{
    $expire_day=date("Y-m-d H:i:s",strtotime("+".$coupon['expire_day']." day "));
}

$insert_data['ai_end_time']  = strtotime($expire_day);



$insert_data['ai_user_id']     = $_REQUEST['member'];
$insert_data['ai_coupon_id']   = $coupon['nid'];
$insert_data['ai_is_free']     = $coupon['is_free'];
$insert_data['ai_create_time'] = time();//派发时间

if($insert_data['ai_coupon_id'])
{
$sql   = insert_date($table_Prefix . 'coupon_member', $insert_data);
$query = do_query($sql) or die ("error:" . mysqli_error());



//触发发券通知信息
//curl 一个api地址，带上通知内容代号，被通知人，然后就触发了。
$_url=$domain."/api/wechat_message/notice_coupon_sent.php?openid=".$_wechat_member['openid'];
file_get_contents($_url);

}
?>


<script type="text/javascript">

     alert("输入信息已保存，点击确认返回继续操作.");
     document.location.href = "detail-list.php?sys_part=coupon"
</script>


